package com.lsu.observer.practice;

import java.util.ArrayList;
import java.util.List;

/**
 * 鲁肃作为主体类
 *
 * @Author wang suo
 * @Date 2020/10/22 0022 8:19
 * @Version 1.0
 */
public class LuSu implements Subject {

    private Boolean changed;
    private List<Observer> observers;

    public LuSu() {
        this.changed = false;
        this.observers = new ArrayList<>();
    }

    /**
     * 鲁肃装杯
     */
    public void throwCup() {
        this.changed = true;
        System.out.println("鲁肃装杯");
        notifyObservers();
    }

    @Override
    public void addObserver(Observer observer) {
        if (!observers.contains(observer)) {
            observers.add(observer);
        }
    }

    @Override
    public void deleteObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update();
        }
    }
}
